package com.jwdf.uda.fileupload.handler;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.jwdf.uda.fileupload.bean.ProgressObj;
import com.jwdf.uda.fileupload.listener.impl.UploadProgressListener;

import java.lang.ref.WeakReference;

/**
 * Created by Administrator on 2016/7/27.
 */
public abstract class ProgressUIHandler extends Handler {
    public static final int UPDATE = 0x01;
    public static final int START = 0x02;
    public static final int FINISH = 0x03;
    private final WeakReference<UploadProgressListener> mUIListenerWeakReference;

    public ProgressUIHandler(UploadProgressListener uiProgressListener) {
        super(Looper.getMainLooper());
        mUIListenerWeakReference = new WeakReference<>(uiProgressListener);
    }

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case UPDATE: {
                UploadProgressListener listener = mUIListenerWeakReference.get();
                if (listener != null) {
                    ProgressObj obj = (ProgressObj) msg.obj;
                    onProgress(listener, obj.getCurrentBytes(), obj.getContentLength());
                }
                break;
            }
            case START: {
                UploadProgressListener listener = mUIListenerWeakReference.get();
                if (listener != null) {
                    onStart(listener);
                }
                break;
            }
            case FINISH: {
                UploadProgressListener listener = mUIListenerWeakReference.get();
                if (listener != null) {
                    onFinish(listener);
                }
                break;
            }
            default:
                super.handleMessage(msg);
                break;
        }
    }

    public abstract void onStart(UploadProgressListener listener);

    public abstract void onProgress(UploadProgressListener listener, long currentBytes, long contentLength);

    public abstract void onFinish(UploadProgressListener listener);
}
